package com.chinaedu.oc.utils;

import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils extends org.apache.commons.lang3.StringUtils {
	
	/**
	 * SQL like语句检索添加模糊匹配符
	 *
	 * @param str
	 * @return
	 */
	public static String parseLikeQuery(String str) {
		str = trimToEmpty(str).replaceAll(" +|　+", "%");
		return "%" + str + "%";
	}
	
	/**
	 * StringUtils.appendStartWith(null,null) = null <br />
	 * StringUtils.appendStartWith("str",null) = "str" <br />
	 * StringUtils.appendStartWith(null,"pre") = "pre" <br />
	 * StringUtils.appendStartWith("prestr","pre") = "prestr" <br />
	 * StringUtils.appendStartWith("str","pre") = "prestr" <br />
	 *
	 * @param str
	 * @param prefix
	 * @return
	 */
	public static String appendStartWith(String str, String prefix) {
		if (prefix == null) {
			return str;
		}
		if (isEmpty(str)) {
			return prefix;
		}
		if (startsWith(str, prefix)) {
			return str;
		} else {
			return prefix + str;
		}
	}
	
	/**
	 * StringUtils.appendEndWith(null,null) = null <br />
	 * StringUtils.appendEndWith("str",null) = "str" <br />
	 * StringUtils.appendEndWith(null,"pre") = "pre" <br />
	 * StringUtils.appendEndWith("strpre","pre") = "strpre" <br />
	 * StringUtils.appendEndWith("str","pre") = "strpre" <br />
	 *
	 * @param str
	 * @param prefix
	 * @return
	 */
	public static String appendEndWith(String str, String prefix) {
		if (prefix == null) {
			return str;
		}
		if (isEmpty(str)) {
			return prefix;
		}
		if (endsWith(str, prefix)) {
			return str;
		} else {
			return str + prefix;
		}
	}
	
	/**
	 * 防止sql注入
	 *
	 * @param sql
	 * @return
	 */
	public static String transactSQLInjection(String sql) {
		return sql.replaceAll(".*([';]+|(--)+).*", " ");
	}
	
	public static boolean checkSQLIN(String ids) {
		return (!isEmpty(ids) && ids.matches("^\\d+(,\\d+)*$"));
	}
	
	/**
	 * 根据平台的不同获取不同的换行符。 windows平台的换行符为/r/n; linux平台的换行符为/n;
	 *
	 * @return
	 */
	public static String newline() {
		return System.getProperty("line.separator");
	}
	
	/**
	 * 截取部分字符串，这里一个汉字的长度认为是2
	 *
	 * @param str
	 * @param len
	 * @return
	 */
	public static String getPartString(String str, int len) {
		try {
			byte b[];
			int counterOfDoubleByte = 0;
			b = str.getBytes("GBK");
			if (b.length <= len) {
				return str;
			}
			for (int i = 0; i < len; i++) {
				if (b[i] < 0) {
					counterOfDoubleByte++;
				}
			}
			if (counterOfDoubleByte % 2 == 0) {
				return new String(b, 0, len, "GBK") + "...";
			} else {
				return new String(b, 0, len - 1, "GBK") + "...";
			}
		} catch (Exception e) {
			return str;
		}
	}
	
	/**
	 * 去除html标签，并进行URLEncode编码
	 *
	 * @param str
	 * @return
	 */
	public static String trimHtmlAndURLEncode(String str) {
		str = str.replaceAll("</?[^>]+>", "");
		return EncodeUtils.urlEncode(str);
	}
	
	/**
	 * 验证用户的手机号是否正确
	 *
	 * @param phoneNum
	 * @return
	 */
	public static boolean checkPhoneNum(String phoneNum) {
		return phoneNum.matches("^\\d{11}$");
	}
	
	/**
	 * 验证email格式是否正确
	 *
	 * @param email
	 * @return
	 */
	public static boolean checkEmail(String line) {
		Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		Matcher m = p.matcher(line);
		return m.find();
	}
	
	public static String notNull(String param) {
		return param == null ? "" : param.trim();
	}
	
	/**
	 * 得到随机密钥
	 *
	 * @param size 密钥长度
	 * @return
	 */
	public static String getRandomString(int size) {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+=-}{][~";
		Random random = new Random((new Date()).getTime());
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < size; i++) {
			int number = random.nextInt(77);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}
}
